package com.futvan.z.erp.customer;

import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.StringUtil;
import com.futvan.z.framework.util.enums.ZxUtil;
import com.futvan.z.shop.shop.s_shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

@Controller
public class CustomerAction extends SuperAction{
	@Autowired
	private CustomerService customerService;

	/**
	 * 打开退款页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ReplaceCustomerMainNumber")
	public ModelAndView ReplaceCustomerMainNumber() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/ReplaceCustomerMainNumber");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}

	/**
	 * 打开导入直销会员
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ImportZXCustomer")
	public ModelAndView ImportZXCustomer() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/ImportZXCustomer");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}


	/**
	 * 打开客户通知发送工具
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/sendCustomerNotices")
	public ModelAndView sendCustomerNotices() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/sendCustomerNotices");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}

	/**
	 * 打开客户通知短信发送工具
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/sendCustomerMessage")
	public ModelAndView sendCustomerMessage() throws Exception {
		ModelAndView model = new ModelAndView("erp/customer/sendCustomerNoticesMessage");
		if(isMobile()) {
			model.addObject("is_mobile", "1");
		}
		return model;
	}


	
	@RequestMapping(value="/customerLogin")
	public @ResponseBody Result Service(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
 		Result authority = z.isServiceAuthority(bean,request);
 		if(Code.SUCCESS.equals(authority.getCode())){
 			String username = bean.get("username");
 			String password = bean.get("password");
 			if(z.isNotNull(username)){
 				if(z.isNotNull(password)){
 					crm_customer cq = new crm_customer();
 					cq.setUsername(username);
 					cq.setIs_enabled("1");
 	 				crm_customer c = sqlSession.selectOne("crm_customer_select", cq);
 	 				String jiami_password = StringUtil.CreatePassword(password);
 	 				if(jiami_password.equals(c.getPassword_login())) {
 	 					result.setCode(Code.SUCCESS);
 	 					result.setData(c);
 	 				}else {
 	 					result.setCode(Code.ERROR);
 	 	 				result.setMsg("密码错误");
 	 				}
 	 			}else {
 	 				result.setCode(Code.ERROR);
 	 				result.setMsg("password is null");
 	 			}
 			}else {
 				result.setCode(Code.ERROR);
 				result.setMsg("username is null");
 			}
 		}else {
 			result = authority;
 		}
		return result;
	}



	@RequestMapping(value="/SendCustomerToZX")
	public @ResponseBody Result SendCustomerToZX(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
		Result authority = z.isServiceAuthority(bean,request);
		if(Code.SUCCESS.equals(authority.getCode())){
			String type = bean.get("type");
			String zids = bean.get("zids");
			String sqlzids = StringUtil.ListToSqlInArray(zids);
			String sql = "select * from crm_customer where zid in ("+sqlzids+")";
			List<crm_customer> c = sqlSession.selectList("crm_customer_select_sql", sql);
			for (crm_customer customer : c) {
				ZxUtil.SendCustomerToZX(type, customer);
			}
			result.setCode(Code.SUCCESS);
			result.setData(c);
		}else {
			result = authority;
		}
		return result;
	}


	/**
	 * 用客户信息同步生成或更新店铺信息
	 * @param bean
	 * @return
	 */
	@RequestMapping(value="/synchronization_shop")
	public @ResponseBody Result synchronization_shop(@RequestParam HashMap<String, String> bean) {
		Result result = new Result();
		String zids = bean.get("zids");
		if(z.isNotNull(zids)){
			String [] zidsarray = zids.split(",");
			for (String zid : zidsarray) {

				crm_customer c = sqlSession.selectOne("crm_customer_select_zid",zid);
				if(z.isNotNull(c)){
					//判读是否有店铺信息
					List<s_shop> list = sqlSession.selectList("s_shop_select_sql","select * from s_shop where customid = '"+zid+"'");
					if(z.isNotNull(list) && list.size()>0){
						//执行更新
						sqlSession.update("update","update s_shop set name = '"+c.getName()+"' ,tel='"+c.getTel()+"',welcome='欢迎光临',info='欢迎进入"+c.getName()+"的微店' where customid = '"+zid+"'");
					}else{
						//执行新增
						s_shop s = new s_shop();
						String shopnumber = z.CreateShopNumber();
						s.setZid(shopnumber);
						s.setNumber(shopnumber);
						s.setName(c.getName());
						s.setTel(c.getTel());
						s.setCustomid(c.getZid());
						s.setWelcome("欢迎光临");
						s.setInfo("欢迎进入"+c.getName()+"的微店");
						sqlSession.insert("s_shop_insert",s);
					}
				}





			}
			result.setSuccess();
		}else{
			result.setError("zids is null");
		}
		return result;
	}


    /**
     * 变更创建时间 1949-01-01 00:00:00
	 * @param bean
     * @return
     * @throws Exception
	 */
	@RequestMapping(value="/update_create_time_1949")
	public @ResponseBody Result update_create_time_1949(@RequestParam HashMap<String,String> bean) throws Exception {
		Result result = new Result();
		try {
			//获取所有客户主键
			String zids = bean.get("zids");
			String[]zid_array = zids.split(",");
			//遍历所有客户主键
			for (int i = 0; i < zid_array.length; i++) {
				String zid = zid_array[i];
				if(z.isNotNull(zid.trim())){
					String sql = "update crm_customer set create_time = '1949-01-01 00:00:00' where zid = '"+zid+"'";
					//变更创建时间
					sqlSession.update("update", sql);
				}
			}
			result.setCode(Code.SUCCESS);
			result.setMsg("变更创建时间成功");
		} catch (Exception e) {
			result.setCode(Code.ERROR);
			result.setMsg("变更创建时间出错|"+e.getMessage());
		}
		return result;
	}


}
